/**
 * Copyright 2018 asiainfo Inc.
 **/
package com.myz.app.shiro.listener;

import com.myz.app.shiro.session.CachingShiroSessionDao;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 对所有的session监听，而不是针对某个特殊的session
 * 用户退出后，Session没有从Redis中销毁，虽然当前重新new了一个，但会对统计带来干扰，通过SessionListener解决这个问题
 * @author maoyz on 2018/6/11
 * @version: v1.0
 */
public class ShiroSessionListener implements SessionListener {

  private final Logger logger = LoggerFactory.getLogger(ShiroSessionListener.class);

  @Autowired
  private CachingShiroSessionDao sessionDao;

  /**
   * 会话创建时触发
   * @param session
   */
  @Override
  public void onStart(Session session) {
    logger.debug("++++++++++++++++ session onStart :" + session.getId());
  }

  /**
   * 会话结束
   * @param session
   */
  @Override
  public void onStop(Session session) {
    sessionDao.delete(session);
    logger.debug("++++++++++++++++ session onStop :" + session.getId());
  }

  /**
   * 会话超时、过期时触发
   * @param session
   */
  @Override
  public void onExpiration(Session session) {
    sessionDao.delete(session);
    logger.debug("++++++++++++++++ session onExpiration :" + session.getId());
  }
}
